package com.my.util;

import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.fastjson2.JSONObject;
import com.my.request.AppChatAddReq;
import com.my.result.ChatMessageResult;
import com.my.result.MessageResult;
import com.my.result.WecomAccessTokenResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import java.time.LocalDateTime;
import java.util.Collections;

public class AccessTokenUtil {

    private static String accessToken;

    private static LocalDateTime expiresAt;

    public static String getToken(){
        if(StringUtils.isBlank(accessToken) || LocalDateTime.now().isAfter(expiresAt)){
            WecomAccessTokenResult wecomAccessTokenResult = getTokenByWeComUrl();
            accessToken = wecomAccessTokenResult.getAccess_token();
            expiresAt = LocalDateTime.now().plusSeconds(wecomAccessTokenResult.getExpires_in() - 10);
        }
        return accessToken;
    }


    private static WecomAccessTokenResult getTokenByWeComUrl(){
        String corpId = SpringUtil.getProperty("wecom.corpId");
        String corpsecret = SpringUtil.getProperty("wecom.corpsecret");
        RestTemplate restTemplate = SpringUtil.getBean(RestTemplate.class);
        ParameterizedTypeReference<WecomAccessTokenResult> result = new ParameterizedTypeReference<WecomAccessTokenResult>() {};
        ResponseEntity<WecomAccessTokenResult> result1 = restTemplate.exchange("https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=" + corpId + "&corpsecret=" + corpsecret, HttpMethod.GET,null,result);
        return result1.getBody();
    }

    public static MessageResult addAppIdByAppchatId(String chatid,String appId){
        String token = getToken();
        RestTemplate restTemplate = SpringUtil.getBean(RestTemplate.class);

        JSONObject param = new JSONObject();
        param.put("chatid",chatid);
        param.put("add_user_list", Collections.singletonList(appId));
        ParameterizedTypeReference<MessageResult> result = new ParameterizedTypeReference<MessageResult>() {};
        ResponseEntity<MessageResult> result1 = restTemplate.exchange("https://qyapi.weixin.qq.com/cgi-bin/appchat/update?access_token=" + token, HttpMethod.POST,new HttpEntity<>(param),result);
        return result1.getBody();
    }

    public static ChatMessageResult addChatId(AppChatAddReq request){
        String token = getToken();
        RestTemplate restTemplate = SpringUtil.getBean(RestTemplate.class);
        ParameterizedTypeReference<ChatMessageResult> result = new ParameterizedTypeReference<ChatMessageResult>() {};
        ResponseEntity<ChatMessageResult> result1 = restTemplate.exchange("https://qyapi.weixin.qq.com/cgi-bin/appchat/create?access_token=" + token, HttpMethod.POST,new HttpEntity<>(request),result);
        return result1.getBody();
    }

}
